package com.imooc.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * FFMPEG模块的测试
 *
 * @author Zhou
 * @create 2020-11-20 14:33
 */
public class FFMPEGUtils {
    private String ffmpegExe;
    private String noVideoOutputPath;

    public FFMPEGUtils(String ffmpegExe, String noVideoOutputPath) {
        this.ffmpegExe = ffmpegExe;
        this.noVideoOutputPath = noVideoOutputPath;
    }

    public FFMPEGUtils(String ffmpegExe) {
        this.ffmpegExe = ffmpegExe;
    }

    /**
     * 去除视频的背景音
     *
     * @param videoInputPath
     * @throws IOException
     */
    public void convertToNoVideo(String videoInputPath) throws IOException {
        List<String> command = new ArrayList<>();
        command.add(ffmpegExe);
        command.add("-i");
        command.add(videoInputPath);
        command.add("-c:v");
        command.add("copy");
        command.add("-an");
        command.add(noVideoOutputPath);

        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = builder.start();

        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        //读取errorString
        String line = "";
        while ((line = br.readLine()) != null) {
        }

        //关闭
        if (br != null) {
            br.close();
        }

        if (inputStreamReader != null) {
            inputStreamReader.close();
        }

        if (errorStream != null) {
            errorStream.close();
        }

    }


    /**
     * 视频和合成背景音
     *
     * @param videoInputPath
     * @param mp3InputPath
     * @param videoOutputPath
     * @param duration
     * @throws IOException
     */
    public void convert(String videoInputPath, String mp3InputPath, String videoOutputPath, double duration) throws IOException {
        // ffmpeg.exe -i 1.mp4 -c:v copy -an noaudio.mp4 : 去除音轨
        // fmpeg.exe -i noaudio.mp4 -i Unravel.mp3 -t 5 -y new_2.mp4 ： 音频视频合成
        convertToNoVideo(videoInputPath);

        List<String> command = new ArrayList<>();
        command.add(ffmpegExe);
        command.add("-i");
        command.add(noVideoOutputPath);
        command.add("-i");
        command.add(mp3InputPath);
        command.add("-t");
        command.add(String.valueOf(duration));
        command.add("-y");
        command.add(videoOutputPath);

        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = builder.start();

        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);


        //读取errorString
        String line = "";
        while ((line = br.readLine()) != null) {
        }


        //关闭
        if (br != null) {
            br.close();
        }

        if (inputStreamReader != null) {
            inputStreamReader.close();
        }

        if (errorStream != null) {
            errorStream.close();
        }

    }

    /**
     * 将视频中1秒的帧作为封面保存
     * @param videoInputPath
     * @param coverOutputPath
     * @throws IOException
     */
    public void getCoverFromVideo(String videoInputPath, String coverOutputPath) throws IOException {
        //ffmpeg -ss 00:00:01 -y -i 1.mp4 -vframes 1 new.jpg
        List<String> command = new ArrayList<>();
        command.add(ffmpegExe);
        command.add("-ss");
        command.add("00:00:01");
        command.add("-y");
        command.add("-i");
        command.add(videoInputPath);
        command.add("-vframes");
        command.add("1");
        command.add(coverOutputPath);

        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = builder.start();

        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);


        //读取errorString
        String line = "";
        while ((line = br.readLine()) != null) {
        }


        //关闭
        if (br != null) {
            br.close();
        }

        if (inputStreamReader != null) {
            inputStreamReader.close();
        }

        if (errorStream != null) {
            errorStream.close();
        }


    }


//    public static void main(String[] args) {
//        FFMPEGUtils ffmpeg = new FFMPEGUtils("D:/InstallationPath/ffmpeg/ffmpeg/bin/ffmpeg.exe");
//        try {
//            ffmpeg.getCoverFromVideo("D:/InstallationPath/ffmpeg/ffmpeg/bin/1.mp4",
//                    "D:/InstallationPath/ffmpeg/ffmpeg/bin/picture.jpg"
//            );
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

}
